#include "MainWidget.h"
#include "ui_MainWidget.h"


MainWidget::MainWidget(QWidget *parent)
        :QWidget(parent), ui(new Ui::MainWidget)
{
    ui->setupUi(this);

    // 设置无边框
    this->setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint);
    this->setProperty("CanMove", true);

    Init();
    OnConnect();
}

MainWidget::~MainWidget()
{
    if (business_thread_ != nullptr)
    {
        business_thread_->Stop();
        business_thread_->quit();
        business_thread_->wait();
        delete business_thread_;
        business_thread_ = nullptr;
    }

    delete ui;
}

bool MainWidget::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == ui->toolButton_close) {
        /* 鼠标进入 */
        if (event->type() == QEvent::Enter) {
            ui->toolButton_close->setIcon(QIcon(":/svg/close_white.svg"));
        }
        // 鼠标离开
        if (event->type() == QEvent::Leave) {
            ui->toolButton_close->setIcon(QIcon(":/svg/close.svg"));
        }
    }

    return QObject::eventFilter(obj, event);
}

void MainWidget::Init()
{
    ui->splitter->setStretchFactor(0, 1);
    ui->splitter->setStretchFactor(1, 2);
    ui->toolButton_close->installEventFilter(this);
    business_thread_= new BusinessThread(this);
    ui->widget_login_data->SetNetworkList(business_thread_->GetNetworkList());
}

void MainWidget::OnConnect()
{
    connect(ui->toolButton_close, &QToolButton::clicked, this, &MainWidget::close);
    connect(ui->toolButton_min, &QToolButton::clicked, this, &MainWidget::showMinimized);
    connect(ui->toolButton_max, &QToolButton::clicked, this, [this]() {
        if (this->isMaximized()) {this->showNormal(); } else { this->showMaximized();}});
    connect(ui->toolButton_set,&QToolButton::clicked,this,&MainWidget::OnSetButtonClicked);
    connect(ui->toolButton_home,&QToolButton::clicked,this,&MainWidget::OnHomeButtonClicked);
    connect(ui->toolButton_data,&QToolButton::clicked,this,&MainWidget::OnDataButtonClicked);
    connect(ui->widget_login_data,&LoginDataWidget::StartServer,this,&MainWidget::OnStartServer);
    connect(business_thread_,&BusinessThread::SendData,ui->widget_login_data,&LoginDataWidget::AddLog);
}

void MainWidget::OnHomeButtonClicked(const bool check) const
{
    Q_UNUSED(check);
    ui->stackedWidget_right->setCurrentIndex(0);
    ui->toolButton_home->setIcon(QIcon(":/svg/home_green.svg"));
    ui->toolButton_data->setIcon(QIcon(":/svg/dashboard.svg"));
}

void MainWidget::OnDataButtonClicked(const bool check) const
{
    Q_UNUSED(check);
    ui->stackedWidget_right->setCurrentIndex(2);
    ui->toolButton_data->setIcon(QIcon(":/svg/dashboard_green.svg"));
    ui->toolButton_home->setIcon(QIcon(":/svg/home.svg"));
}

void MainWidget::OnSetButtonClicked(const bool check) const
{
    Q_UNUSED(check);
    ui->stackedWidget_right->setCurrentIndex(1);
    ui->toolButton_home->setIcon(QIcon(":/svg/home.svg"));
    ui->toolButton_data->setIcon(QIcon(":/svg/dashboard.svg"));
}

void MainWidget::OnStartServer(const bool check) const
{
    if (check) {
        business_thread_->SetNetworkIndex(ui->widget_login_data->GetCurentNetworkIndex());
        business_thread_->start();
    } else {
        business_thread_->Stop();
    }
}
